const koa = require("koa");
const app = new koa();
const koaBody = require("koa-body");
const router = require("koa-router")();
const cors = require("koa2-cors");

router.post("/login", async ctx => {
    // console.log(ctx.request.body);
    var { username, pass } = ctx.request.body;
    if (username == "dai" && pass == "123456") {
        ctx.cookies.set("loginAuth", true, {
            httpOnly: false
        })
        ctx.body = {
            code: 200,
            msg: "登录成功"
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "登录失败,用户名或密码错误"
        }
    }
})
// app.use(async (ctx,next)=>{
//     ctx.set("Access-Control-Allow-Methods","*");
//     await next()
// })
app.use(cors({
    origin:ctx=>{
        return ctx.headers.origin
    },
    credentials:true,
}));
app.use(koaBody());
app.use(router.routes());
app.listen(9090)
